package com.chen.controller;

import com.chen.pojo.*;
import com.chen.service.LeaveService;
import com.chen.service.DormService;
import com.chen.service.RepairService;
import com.chen.service.StudentService;
import com.chen.utils.Constants;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.swing.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class StudentController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private LeaveService leaveService;
    @Autowired
    DormService dormService;
    @Autowired
    RepairService repairService;

    @RequestMapping("/toStudentInfo.html")
    public String toStudentInfo(HttpServletRequest request){
        Integer role = (Integer)request.getSession().getAttribute("USER_TYPE");
        User teacher =(User) request.getSession().getAttribute("SESSION_USER");
        if(role==1){
            return "/WEB-INF/pages/admin/StudentList";
        }else{
            return "/WEB-INF/pages/teacher/StudentList";
        }

    }
    //查询所有学生
    @RequestMapping("/getStudentList.html")
    @ResponseBody
    public PageInfo<Student> studentList(Student student, @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum){

        System.out.println(pageNum);
        PageHelper.startPage(pageNum, Constants.SPAGESIZE);
        List<Student> students = studentService.getStudentList(student);
        PageInfo<Student> pageInfo = new PageInfo<Student>(students);
        return pageInfo;
    }

    @RequestMapping("deleteStudent.html")
    public String deleteStudent(Integer id){

        studentService.deleteStudent(id);
        return "/WEB-INF/pages/admin/StudentList";
    }
    @RequestMapping("to_updateStudent.html")
    public String getStudentById(Integer id, Model model,HttpServletRequest request){
        Integer role = (Integer)request.getSession().getAttribute("USER_TYPE");
        User teacher = (User) request.getSession().getAttribute("SESSION_USER");
        Student student=studentService.getStudentById(id);
        model.addAttribute("student",student);
        if(role==1){
           return "/WEB-INF/pages/admin/updateStudent";
       }else{
            return "/WEB-INF/pages/teacher/updateStudent";
        }

    }

    @RequestMapping("updateStudent.html")
    @ResponseBody
    public String  updateStudent(Student student){
        try {
            studentService.updateStudent(student);
            return "success";
        }catch (Exception e){
            e.printStackTrace();
            return "shibai";
        }

    }

    @RequestMapping("/todormexchange.html")
    public String dormexchange(){
        return "/WEB-INF/pages/admin/todormexchange";
    }

    @RequestMapping("/to_dormexchange.html")
    @ResponseBody
    public String toDormExchange(String sno){
        if(sno == ""){
            return "请输入学号";
        }
        if(studentService.getStudentBySno(sno) != null){
            return "ok";

        }
        return "学生不存在";

    }

    @RequestMapping("/dormexchange.html")
    public String dormExchange(String sno, HttpServletRequest request){
        Student student = studentService.getStudentBySno(sno);
        Dorm dorm = dormService.getDormById(student.getDid());
        student.setDno(dorm.getDno());
        request.setAttribute("student",student);
        return "/WEB-INF/pages/admin/dormexchange";
    }

    //学生迁出登记
    @RequestMapping("/toLeave.html")
    public String toLeave(){
        return "/WEB-INF/pages/admin/toLeave";
    }

    @RequestMapping("leaveLog.html")
    public String leaveLog(String sno,HttpServletRequest request){
        String message = "";
        Student student  =studentService.getStudentBySno(sno);
        if( student== null){
            message = "没有此学生！";
            request.setAttribute("noStudent",message);
            return "/WEB-INF/pages/admin/toLeave";
        }else{
            request.setAttribute("student",student);
            return "/WEB-INF/pages/admin/doLeave";
        }
    }

    @RequestMapping("deleteStudentByLeave.html")
    @ResponseBody
    public String deleteStudent(Leave leave){
        try {
            studentService.deleteStudent(leave.getSid());
            Date date = new Date();
            leave.setCreateDate(date);
            leaveService.addLeave(leave);
            return "success";
        }catch (Exception e){
            e.printStackTrace();
            return "shibai";
        }
    }




    @RequestMapping("/getDormByBuilding.html")
    @ResponseBody
    public String getDormByBuilding(Integer bid){
        Building building = new Building();
        building.setId(bid);
        List<Dorm> dormList = dormService.getDormListByAjax(building);
        if(dormList.size() != 0){
            String dorm = dormList.get(0).getDno()+"——"+dormList.get(dormList.size()-1).getDno()+"";
            System.out.println(dorm);
            return dorm;
        }else {
            return "该楼中还没有宿舍";
        }
    }

    @RequestMapping("/isdorm.html")
    @ResponseBody
    public String isdorm(Integer bid,Integer dno){
        Dorm dorm = new Dorm();
        dorm.setDno(dno);
        dorm.setBid(bid);
        List<Dorm> dormList = dormService.getDormList(dorm);
        if(dormList.size() == 0){
            return "没有这个宿舍";
        }else {
            if(dormList.get(0).getSnum() == dormList.get(0).getLiveNum()){
                return "该宿舍已满";
            }
        }
        return "conection";
    }
    @RequestMapping("/changedorm.html")
    @ResponseBody
    public String changeDorm(Integer studentId,Integer bid,Integer dno,HttpServletRequest request){
        User user = (User) request.getSession().getAttribute(Constants.SESSION_USER);
        Student stu = studentService.getStudentById(user.getId());
        //调换寝室
        Dorm dorm = new Dorm();
        dorm.setBid(bid);
        dorm.setDno(dno);
        Dorm getDorm = dormService.getDormList(dorm).get(0);
        Student student = new Student();
        student.setBid(bid);
        student.setDid(getDorm.getId());
        student.setId(studentId);
        try {
            studentService.updateDormByStudentId(student);
            return "调换成功";
        }catch (Exception e){
            e.printStackTrace();
            return "调换失败";
        }
    }

    @RequestMapping("/to_studentAbsenceList.html")
    public String toStudentAbsenceList(){
        System.out.println("去学生缺勤页面");
        return "/WEB-INF/pages/student/studentAbsenceList";
    }

    //根据学生id获取学生缺勤记录
    @RequestMapping("/getStudentAbsenceRecord.html")
    @ResponseBody
    public PageInfo<MyAbsenceRecord> gsar(@RequestParam(defaultValue = "1")Integer pageNum,String createDate,HttpServletRequest request){
        Absence absence = new Absence();
        if(createDate != null && createDate != ""){
            absence.setCreateDate(createDate);
        }
        User user = (User) request.getSession().getAttribute("SESSION_USER");
        int studentId = user.getId();
        absence.setSid(studentId);
        PageHelper.startPage(pageNum,Constants.PAGESIZE);
        List<MyAbsenceRecord> myAbsenceRecordList = studentService.getMyAbsenceRecord(absence);
        PageInfo<MyAbsenceRecord> pageInfo = new PageInfo<MyAbsenceRecord>(myAbsenceRecordList);
        if(myAbsenceRecordList.size() == 0){
            return null;
        }else{
            return pageInfo;
        }
    }

    //跳转报修页面
    @RequestMapping("/to_repair.html")
    public String toRepair(){
        return "/WEB-INF/pages/student/repairList";
    }

    //显示报修列表
    @RequestMapping("/showRepair.html")
    @ResponseBody
    public PageInfo<RepairInfo> showRepair(@RequestParam(defaultValue = "1")Integer pageNum, String createDate, HttpServletRequest request){
        Repair repair = new Repair();
        User user = (User) request.getSession().getAttribute("SESSION_USER");
        if(createDate != null && createDate != ""){
            repair.setCreateTime(createDate);
        }
        int did = studentService.getStudentById(user.getId()).getDid();
        repair.setDid(did);
        PageHelper.startPage(pageNum, Constants.PAGESIZE);
        List<RepairInfo> repairInfoList = repairService.getRepairList(repair);
        PageInfo<RepairInfo> pageInfo = new PageInfo<RepairInfo>(repairInfoList);
        for (RepairInfo repairInfo : pageInfo.getList()) {
            if(repairInfo.getStatus() == null){
                repairInfo.setStatusInfo("待修理");
            }else{
                repairInfo.setStatusInfo("已修理");
            }
            if(repairInfo.getTname() == null){
                repairInfo.setTeacher("还未有老师确认");
            }else{
                repairInfo.setTeacher(repairInfo.getTname());
            }
        }
        return pageInfo;
    }

    @RequestMapping("/addRepair.html")
    @ResponseBody
    public String addRepair(String rtype,HttpServletRequest request){
        System.out.println(rtype);
        User user = (User) request.getSession().getAttribute("SESSION_USER");
        Repair repair = new Repair();
        Student student = studentService.getStudentById(user.getId());
        repair.setRtype(rtype);
        repair.setSid(student.getId());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        repair.setCreateTime(simpleDateFormat.format(new Date()));
        repair.setBid(student.getBid());
        repair.setDid(student.getDid());
        try {
            repairService.addRepair(repair);
            return "success";
        }catch (Exception e){
            e.printStackTrace();
            return "defeated";
        }
    }
}
